package com.dingreading.cloud.subject.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.util.JsonUtil;
import com.dingreading.cloud.subject.entity.Subjects;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TreeJsonUtil {

    /**
     * 创建一颗树，以json字符串形式返回
     *
     * @param list 原始数据列表
     * @return 树
     */
    public static JSONArray createTreeJson(List<Subjects> list) {
        JSONArray rootArray = new JSONArray();

        LinkedHashMap<Long, List<Subjects>> map = list.stream().collect(
                Collectors.groupingBy(Subjects::getSubjectMainId, LinkedHashMap::new, Collectors.toList()));
        if (!map.isEmpty()) {
            for (Map.Entry<Long, List<Subjects>> entry : map.entrySet()) {
                List<Subjects> subjectsList = entry.getValue();
                if (CollectionUtils.isNotEmpty(subjectsList)) {
                    Subjects root = null;
                    List<Subjects> childList = new ArrayList<>();
                    for (Subjects subjects : subjectsList) {
                        if (subjects.getParentId() == null || subjects.getParentId() == 0) {
                            root = subjects;
                        } else {
                            childList.add(subjects);
                        }
                    }

                    if (null != root) {
                        JSONObject rootObj = createBranch(root, childList);
                        rootArray.add(rootObj);
                    }
                }
            }
        }

        return rootArray;
    }

    /**
     * 递归创建分支节点Json对象
     *
     * @param subjectsList 创建树的原始数据
     * @param root         当前节点getSubject
     * @return 当前节点与该节点的子节点json对象
     */
    private static JSONObject createBranch(Subjects root, List<Subjects> subjectsList) {
        /*
         * 将javabean对象解析成为JSON对象
         */
        JSONObject currentObj = JsonUtil.toJsonObject(root);
        JSONArray childArray = new JSONArray();
        /*
         * 循环遍历原始数据列表，判断列表中某对象的父id值是否等于当前节点的id值，
         * 如果相等，进入递归创建新节点的子节点，直至无子节点时，返回节点，并将该 节点放入当前节点的子节点列表中
         */
        if (CollectionUtils.isNotEmpty(subjectsList))
            for (Subjects newNode : subjectsList) {
                if (newNode.getParentId() != null && newNode.getParentId().equals(root.getId())) {
                    JSONObject childObj = createBranch(newNode, subjectsList);
                    childArray.add(childObj);
                }
            }

        /*
         * 判断当前子节点数组是否为空，不为空将子节点数组加入children字段中
         */
        currentObj.put("childList", childArray);
        return currentObj;
    }


    /**
     * 找寻指定菜单的下级菜单
     *
     * @param menuEntity 当前菜单
     * @param list       所有的菜单list
     * @return 下级菜单
     */
    public static List<Subjects> fromTree(Subjects menuEntity, List<Subjects> list) {
        return list.stream()
                .filter(ele -> menuEntity.getId().equals(ele.getParentId()))
                .collect(Collectors.toList());
    }

}
